define (require,exports,module)->
	template = require 'tpl/template'
	Survey = Backbone.View.extend
		el:'body'
		events:
			'click .btn-question-edit':'editQuestion'
			'click .btn-question-add':'addQuestion'
			'click .btn-question-del':'delQuestion'
			'click .btn-change-to-input':'changeToInput'
			'click .btn-change-to-select':'changeToSelect'
			'change .checkbox-multiselect':'switchMultiSelect'
		renderList:(data)->
			html = ''
			html += template('survey_list',item) for item in data.list
			console.log html
			$('#survey-list tbody').html html

		renderSurvey:(data)->
			console.log data
			$("#survey-name").val data.survey.name
			if data.survey.description == null or data.survey.description == ''
				$("#survey-description").html '暂无描述'
			else
				$("#survey-description").html data.survey.description
			html = ''
			html += @renderQuestion q for q in data.questions
			$('#survey_questions').html html

		renderQuestion:(data)->
			html = ''
			html += template 'survey_question_view',data
			html

		renderOption:(data)->
			html = ''
			html += template 'survey_option_view',data
			html

		getQuestion:(e)->
			data =
				id:""
				name:""
				answer: null
				description: null
				id: "19"
				limit: ""
				maxselect: "0"
				picture: ""
				survey_id: "2"
				tag: "拜访信息"
				type: "1"
				options: []

			$('.option').each (index)->
				data.options.push @getOption $(this)
				
		getOption:(e)->
			data =
				id: "68"
				isanswer: null
				name: "有"
				picture: ""
				scroe: "0"
				survey_id: "1"
				survey_question_id: "19"

		addQuestion:()->
			data =
				id:""
				name:"新的题目"
				answer: null
				description: null
				id: "19"
				limit: ""
				maxselect: "0"
				picture: ""
				survey_id: "2"
				tag: "拜访信息"
				type: "1"
				options: []
			o =
				id: "0"
				isanswer: "0"
				name: "选项名称"
				picture: ""
				scroe: "0"
				survey_id: @getSurveyId()
				survey_question_id: "0"

			data.options.push o
			data.options.push o

			html = ''
			html += template 'survey_question_edit',data
			$("#survey_questions").append html

			$('.btn-question-add').hide()

		getSurveyId:()->
			$("#survey-id").val()

		changeToSelect:()->
			$(".btn-change-to-select").hide()
			$(".btn-change-to-input").show()
			$("#option-select").show()
			$("#option-input").hide()
			$("#question-option-list").show()

		changeToInput:()->
			$(".btn-change-to-input").hide()
			$(".btn-change-to-select").show()
			$("#option-select").hide()
			$("#option-input").show()
			$("#question-option-list").hide()

		switchMultiSelect:()->
			if $(".checkbox-multiselect:checked").length is 0
				$(".select-limit").show();
			else
				$(".select-limit").hide();
